package register;

import android.content.Context;
import android.media.MediaPlayer;
import android.os.Message;
import android.widget.Toast;

import com.example.administrator.newxlmedical.R;

import constants.Constants;
import constants.HandlerConstant;
import customhandler.MyHandler;
import singleton.SingletonClass;
import utils.MediaPlayerUtils;
import utils.ToastUtil;

/**
 * Created by Administrator on 2018/5/17.
 */

public abstract class FingerprintRegisterClass {
    public Context mContext;
    public MyHandler allHandler;
    public MediaPlayer mediaPlayer;

    public FingerprintRegisterClass(Context mContext) {
        this.mContext = mContext;
        allHandler = SingletonClass.getSingleton(MyHandler.class);
        allHandler.sendEmptyMessage(HandlerConstant.FINGERPRINT_REGISTER_FIRST);
        MediaPlayerUtils.playSound(mContext,R.raw.fingerprint_register_down);
    }

    public void onHandlerMess(Message msg) {
        switch (msg.what) {
            case HandlerConstant.FINGERPRINT_REGISTER_FIRST:
//                ToastUtil.showToast(mContext,"frist");
                break;
            case HandlerConstant.FINGERPRINT_REGISTER_FIRST_RETURN:
//                Toast.makeText(mContext,"识别中...",Toast.LENGTH_SHORT).show();
//                ToastUtil.showToast(mContext,"frist Return");
                allHandler.sendEmptyMessageDelayed(HandlerConstant.FINGERPRINT_REGISTER_SECOND,2000);
                break;
            case HandlerConstant.FINGERPRINT_REGISTER_SECOND:

            break;
            case HandlerConstant.FINGERPRINT_REGISTER_SECOND_RETURN:
                allHandler.sendEmptyMessageDelayed(HandlerConstant.FINGERPRINT_REGISTER_THIRD,2000);
//                ToastUtil.showToast(mContext,"second Return");
                break;
            case HandlerConstant.FINGERPRINT_REGISTER_THIRD:

                break;
            case HandlerConstant.FINGERPRINT_REGISTER_THIRD_RETURN:

//                allHandler.sendEmptyMessage(HandlerConstant.FINGERPRINT_REGISTER_THIRD);
                break;
            case HandlerConstant.FINGERPRINT_REGISTER_ERROR:
                ToastUtil.showToast(mContext,"注册失败");
                resetFingerprint();
                break;
            case HandlerConstant.FINGERPRINT_FEATURE_SUCCESS:
//                ToastUtil.showToast(mContext,"FEATURE_SUCCESS");
                String s = (String) msg.obj;
                String fingerprintUserId = s.substring(0, 4);
                String fingerprintFeature = s.substring(6, s.length());

                //删除指纹
                Message message = allHandler.obtainMessage();
                message.obj = fingerprintUserId;
                message.what = HandlerConstant.DELETE_FINGERPRINT_USER;
                allHandler.sendMessage(message);
                fingerprintNetWork(fingerprintUserId, fingerprintFeature);
                break;
//            case HandlerConstant.DELETE_FINGERPRINT_USER_SUCCESS:
//                allHandler.sendEmptyMessage(HandlerConstant.REGISTER_SUCCESS);
//                break;
//                case HandlerConstant.TRANSFER_FINGERPRINT_FEATURE_SUCCESS:
//                    myFingerprintDialog.dismiss();
//                    ActivityUtil.toastShow(mContext, "注册成功");
//                    myRegisterSuccessDialog.show();
//                    mediaPlayer.release();
//                    mp = MediaPlayer.create(mContext, R.raw.register_success);
//                    mp.start();
//                    break;

            case HandlerConstant.ACK_FAIL:
                ToastUtil.showToast(mContext, "执行失败");
                resetFingerprint();
                break;
            case HandlerConstant.ACK_FULL:
                ToastUtil.showToast(mContext, "数据库已满");
//                resetFingerprint();
                break;
            case HandlerConstant.ACK_TIMEOUT:
                ToastUtil.showToast(mContext, "指纹输入超时");
                break;
//                case HandlerConstant.ACK_BREAK:
//                    ActivityUtil.toastShow(mContext, "当前命令已被终止");
//                    resetFingerprintDialog();
//                    break;
            case HandlerConstant.ACK_IMAGEFIAL:
                ToastUtil.showToast(mContext, "图像不合格");
                break;
        }
    }

    private void resetFingerprint() {
        allHandler.sendEmptyMessage(HandlerConstant.FINGERPRINT_REGISTER_FIRST);
    }

    public abstract void fingerprintNetWork(String fingerprintUserId, String fingerprintFeature);
}
